package org.linlinjava.litemall.core.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import cn.hutool.core.map.MapUtil;

public class ConvertUtils {
	/**
	 * 返回Map Key驼峰转换 xxx_abc-->xxxAbc
	 *
	 * @param sourceList
	 * @param <T>
	 * @return
	 */
	public static <T> List<T> sourceToCamelCaseMap(Collection<?> sourceList) {
		if (sourceList == null) {
			return null;
		}

		List targetList = new ArrayList<>(sourceList.size());
		try {
			for (Object source : sourceList) {
				Map<String, Object> sourceMap = (Map<String, Object>) source;
				targetList.add(MapUtil.toCamelCaseMap(sourceMap));
			}
		} catch (Exception e) {
			return null;
		}

		return targetList;
	}

	/**
	 * Map对象转换实体对象
	 * 
	 * @param map
	 * @param beanClass
	 * @return
	 * @throws Exception
	 */
	public static <T> T mapToObject(Map<String, Object> map, Class<T> beanClass) {
		if (map == null)
			return null;

		T t = null;
		try {
			t = beanClass.newInstance();
			org.apache.commons.beanutils.BeanUtils.populate(t, map);
		} catch (Exception e) {
			return null;
		}

		return t;
	}
}
